package com.example.basic.demo;

import java.util.List;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.coordinatorlayout.widget.CoordinatorLayout;

import com.google.android.material.appbar.AppBarLayout;

public class ScrollBehavior extends CoordinatorLayout.Behavior<View> {

  private int calendarHeight;

  public ScrollBehavior(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  @Override
  public boolean layoutDependsOn(
      @NonNull CoordinatorLayout parent,
      @NonNull View child, @NonNull
      View dependency
  ) {
    return dependency instanceof AppBarLayout;
  }

  @Override
  public boolean onLayoutChild(@NonNull CoordinatorLayout parent, @NonNull View child,
      int layoutDirection) {
    super.onLayoutChild(parent, child, layoutDirection);
    if (calendarHeight == 0) {
      final List<View> dependencies = parent.getDependencies(child);
      for (int i = 0, z = dependencies.size(); i < z; i++) {
        View view = dependencies.get(i);
        if (view instanceof AppBarLayout) {
          calendarHeight = view.getMeasuredHeight();
        }
      }
    }
    child.setTop(calendarHeight);
    child.setBottom(child.getBottom() + calendarHeight);
    return true;
  }
}
